﻿@model IEnumerable<CloudFranceTriple.Models.ForumSet>
@{    ViewBag.Title = CloudFranceTriple.WebRole.Properties.Resources.ForumIndexTitle;
      Layout = "~/Views/Shared/_Layout.cshtml";
      var IsAdminForum = User.IsInRole("RoleAdminForum");
}
<div class="content-padding">
    <h1>
        @CloudFranceTriple.WebRole.Properties.Resources.ForumIndexTitle
    </h1>
    @Html.Action("ShowOnForum", "Annonce", new { area = "Forum" })
    <section class="forumlist">
        <span>
            @{if (IsAdminForum)
              {
                @Html.ActionLink(CloudFranceTriple.WebRole.Properties.Resources.ForumCreateButton, "Create")
                <br />
              }
            }
            <br />
        </span>
        @Html.Partial("_TopicQueriesLinks")
        <script type="text/javascript">
            $(document).ready(function () {
                $("div.content").hide();
                $("h3 span.tooglemark").replaceWith("<span class='tooglemark'>+</span>");

                $("header.heading").click(function () {

                    if ($(this).next("div.content").is(':visible')) {
                        $(this).children("h3").children(".tooglemark").replaceWith("<span class='tooglemark'>+</span>");
                    }
                    else {
                        $(this).children("h3").children(".tooglemark").replaceWith("<span class='tooglemark'>-</span>");
                    }

                    $(this).next("div.content").slideToggle(500);

                });
            });
        </script>
        @foreach (var item in Model)
        {
            var CurrentUserRight = CloudFranceTriple.WebRole.Models.RightLevel.GetRightLevelSelectListItems()[new CloudFranceTriple.WebRole.BaseController().UserRightLevelByForumId(item.Id, User)].Text;
            
            <header class="heading">
                <h3><span class="tooglemark"></span>@Html.DisplayTextFor(modelItem => item.LabelForum).ToString().ToUpper() (@CurrentUserRight)</h3>
                <span>
                    @{if (IsAdminForum)
                      {
                        @Html.ActionLink(CloudFranceTriple.WebRole.Properties.Resources.CategoryCreateButton, "Create", "Category", new { id = item.Id, area = "Forum" }, null)
                        @Html.ActionLink(CloudFranceTriple.WebRole.Properties.Resources.ButtonEdit, "Edit", new { id = item.Id })
                        @Html.ActionLink(CloudFranceTriple.WebRole.Properties.Resources.ButtonSecurity, "Index", "Security", new { id = item.Id, area = "Forum" }, null)
                        @Html.ActionLink(CloudFranceTriple.WebRole.Properties.Resources.ButtonDelete, "Delete", new { id = item.Id })
                        @Html.ActionLink(CloudFranceTriple.WebRole.Properties.Resources.ButtonMoveUp, "MoveUp", new { id = item.Id })
                        @Html.ActionLink(CloudFranceTriple.WebRole.Properties.Resources.ButtonMoveDown, "MoveDown", new { id = item.Id })
                        <br />
                        <br />
                      }
                      else
                      {
                        <br />
                      }
                             
                    }
                </span>
            </header>
            <div class="content">
                @foreach (var category in item.ForumCategorySet.OrderBy(cat => cat.OrderForumCategory))
                {
                    <h4>@Html.ActionLink(HttpUtility.HtmlDecode(category.LabelForumCategory).ToString().ToUpper(), "Index", "Topic", new { id = category.Id }, null)</h4>
                    <p>
                        (@Html.DisplayFor(modelItem => category.NumberOfTopic) @CloudFranceTriple.WebRole.Properties.Resources.ForumTotalTopic)
                (@Html.DisplayFor(modelItem => category.NumberOfMessage) @CloudFranceTriple.WebRole.Properties.Resources.ForumTotalPost)
                    </p>
                    <p>
                        @Html.DisplayTextFor(modelItem => category.DescriptionForumCategory)
                    </p>
                    String _LabelForumTopic = String.Empty;
                    if (category.LastPostTopicId.HasValue)
                    {
                        var topicset = new CloudFranceTriple.Models.cloudfrAboq2RsDMEntities().ForumTopicSet.Find(category.LastPostTopicId);
                        if (topicset != null)
                        {
                            _LabelForumTopic = Server.HtmlDecode(topicset.LabelForumTopic);
                        }
                    }
                    if (_LabelForumTopic != String.Empty)
                    {
                    <p>
                        @Html.ActionLink(String.Format("{0} {1}", CloudFranceTriple.WebRole.Properties.Resources.ForumTopicsItemTitle, _LabelForumTopic), "Index", "Post", new { id = category.LastPostTopicId ?? 0, area = "Forum" }, null)
                    </p>
                    <p>
                        @Html.DisplayFor(modelItem => category.LastPostAuthor) - @Html.DisplayFor(modelItem => category.LastPostCreationDate, "TemplateUserDateTime")
                    </p>
                    }
                    <p>
                        <span>
                            @{if (IsAdminForum)
                              {                    
                                @Html.ActionLink(CloudFranceTriple.WebRole.Properties.Resources.ButtonEdit, "Edit", "Category", new { id = category.Id, area = "Forum" }, null);
                                @Html.ActionLink(CloudFranceTriple.WebRole.Properties.Resources.ButtonDelete, "Delete", "Category", new { id = category.Id, area = "Forum" }, null);
                                @Html.ActionLink(CloudFranceTriple.WebRole.Properties.Resources.ButtonMoveUp, "MoveUp", "Category", new { id = category.Id, area = "Forum" }, null);
                                @Html.ActionLink(CloudFranceTriple.WebRole.Properties.Resources.ButtonMoveDown, "MoveDown", "Category", new { id = category.Id, area = "Forum" }, null);
                                <br />
                              }
                            }
                            <br />
                        </span>
                    </p>
                }
            </div>
        }
        @Html.Partial("_TopicQueriesLinks")
        <span>
            <br />
            @{if (IsAdminForum)
              {
                @Html.ActionLink(CloudFranceTriple.WebRole.Properties.Resources.ForumCreateButton, "Create");
                <br />
              }
            }
            <br />
        </span>
    </section>
    <section>
        @{if ((ViewBag.HasForumPostUnread as Boolean?) ?? false)
          {
            <p>
                <script type="text/javascript" async="async">
                    var ctx;
                    function MarkAllMessageAsReadProgress() {
                        ctx = setInterval(function () { RefreshProgress() }, 5000);
                    }
                    function RefreshProgress() {
                        var _unreadMessage;
                        $.get("/Forum/UnreadMessage/ShowUnreadMessage", function (data) { _unreadMessage = data; })
                        if (_unreadMessage == "0 messages non-lus") { RefreshProgressStop(); }
                        $('#UnreadMessageLabel').replaceWith('<p id="UnreadMessageLabel">' + _unreadMessage + '</p>');
                    }
                    function RefreshProgressStop() {
                        clearInterval(ctx);
                    }
                </script>
                @Ajax.ActionLink(CloudFranceTriple.WebRole.Properties.Resources.ForumMarkAllMessageAsRead, "MarkAllPostAsRead", "UnreadMessage", new { area = "Forum" },
                new AjaxOptions { HttpMethod = "Get", OnBegin = "MarkAllMessageAsReadProgress();" })
            </p>
          } 
        }
        <p id="UnreadMessageLabel">
            @Html.Action("ShowUnreadMessage", "UnreadMessage", new { area = "Forum" })
        </p>
        @ViewBag.ForumFooter
    </section>
</div>
